/****
*作者: livahu
*时间：2007-9-24
*导航栏生成器
*divEle：所要生成导航栏在页面中的位置，一个层（div）中
*xmlUrl：所要解析的xml路径，用来作为生成导航的依据
*hasSeparator：默认为true，即在顶级导航菜单之间加入" | "分隔符
****/

var Navigation = function(divEle, xmlUrl, hasSeparator) {
	if(hasSeparator == null) {hasSeparator = true};
	var ds = new Ext.data.Store({
	
		proxy: new Ext.data.HttpProxy({url: xmlUrl, method: "POST"}),
		reader: new Ext.data.XmlReader({
		record: 'menu',
		id: 'id',
		totalRecords: '@total'
		}, [
		{name: 'id', mapping: 'id'},
		{name: 'text', mapping: 'text'},
		{name: 'parent', mapping: 'parent'},
		{name: 'hasChild', mapping: 'hasChild'},
		{name: 'url', mapping: 'url'}
		])
	});

	ds.on("load",
		 function(dataset) {
			var tb = new Ext.Toolbar(divEle);
			var record = null;
			var tbmap = [];
			for(var i = 0; i < dataset.getTotalCount(); i++) {
				record = dataset.getAt(i);
				id = record.get("id");
				text = record.get("text");
				parent1 = record.get("parent");						
				hasChild = record.get("hasChild");
				url = record.get("url");
				if(parent1 == "root") {
					if(hasChild == "true") {
						tbmap[id] = new Ext.menu.Menu({id:id});
						tb.add({id:id,text:text,menu:tbmap[id]});
						if(hasSeparator) {tb.add("-")}
					} else {
						tb.add({id:id,text:text, handler:turnTo.createDelegate(this,[url])});
						if(hasSeparator) {tb.add("-")}
					}
				} else {
					if(hasChild == "true") {
						tbmap[id] = new Ext.menu.Menu({id:id});
						tbmap[parent1].add({id:id,text:text,menu:tbmap[id]});
					} else {
						tbmap[parent1].add({id:id,text:text,handler:turnTo.createDelegate(this,[url])});
					}
				}
			}

			//定义转向函数
			function turnTo(url) {
				Ext.get('mycontent').dom.src = url;
			}
		}
	 );
	 
	 
	ds.load();	
};
